package com.datam.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.datam.dao.CompanyPkInfoDAO;
import com.datam.domain.CompanyInfoDO;
import com.datam.domain.CompanyPkInfoDO;
import com.datam.domain.SysUserDO;
import com.datam.query.QueryCompanyInfo;
import com.datam.query.QueryCompanyPkInfo;
import com.datam.utils.DMResult;


@Controller
public class PKController {
	
	private static Logger logger = LoggerFactory.getLogger(PKController.class);
	
	@Autowired
	CompanyPkInfoDAO companyPkInfoDAO ; 
	
	
	@RequestMapping("VOCsCompany/GetPKInfoData")
	public @ResponseBody Object GetPKInfoData(HttpServletRequest request, HttpServletResponse response,
			Long ID,String PKType){
		try {
			QueryCompanyPkInfo QueryCompanyPkInfo = new QueryCompanyPkInfo();
			QueryCompanyPkInfo.setCOMPANYID(ID);
			QueryCompanyPkInfo.setTYPE(PKType);
			List< CompanyPkInfoDO > data = companyPkInfoDAO.selectCompanyPkInfoList(QueryCompanyPkInfo);
			Map r = new HashMap();
			r.put("total", data.size());
			r.put("rows", data);
			return r ;
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	@RequestMapping("VOCsCompany/savePKInfoData")
	public @ResponseBody Object savePKInfoData(HttpServletRequest request, HttpServletResponse response,
			CompanyPkInfoDO companyPkInfoDO){
		try {
			if(companyPkInfoDO.getID()==null){
				SysUserDO user = (SysUserDO) request.getSession().getAttribute("user");
				companyPkInfoDO.setCREATEUSER(user.getUserId()+"");
				companyPkInfoDO.setCREATETIME(new Date());
				companyPkInfoDO.setIconCls("vocs_gray");
				companyPkInfoDAO.insertCompanyPkInfo(companyPkInfoDO);
			}else{
				companyPkInfoDAO.updateCompanyPkInfoById(companyPkInfoDO);
			}
			
			return new DMResult("插入成功",true);
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	@RequestMapping("VOCsCompany/GetPKInfoInfoByID")
	public @ResponseBody Object GetPKInfoInfoByID(HttpServletRequest request, HttpServletResponse response,
					long rowID){
		try {
			List data = new ArrayList();
			data.add(companyPkInfoDAO.selectCompanyPkInfoById(rowID));
			return new DMResult(data,"查询成功",true);
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	@RequestMapping("VOCsCompany/DelPKInfoInfoByID")
	public @ResponseBody Object DelPKInfoInfoByID(HttpServletRequest request, HttpServletResponse response,
			long id){
		try {
			companyPkInfoDAO.deleteById(id);
			return new DMResult("删除成功",true);
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	
	@RequestMapping("VOCsCompany/GetPKDataByID")
	public @ResponseBody Object GetPKDataByID(HttpServletRequest request, HttpServletResponse response,
					long id){
		try {
			List<Map> d = new ArrayList<Map>();
			QueryCompanyPkInfo QueryCompanyPkInfo = new QueryCompanyPkInfo();
			QueryCompanyPkInfo.setCOMPANYID(id);
			List< CompanyPkInfoDO >  data = companyPkInfoDAO.selectCompanyPkInfoList(QueryCompanyPkInfo);
			if(data ==null || data.size() ==0)
				return d;
			
			for(CompanyPkInfoDO c : data){
				Map tmp = new HashMap();
				tmp.put("CODE", c.getID());
				tmp.put("TITLE",c.getNAME());
				d.add(tmp);
			}
			return d;
		}catch (Exception e) {
			e.printStackTrace();
			return new DMResult("操作失败",false);
		}
	}
	
	


}
